/**
 * 商家相关接口
 * 
 */
const express = require("express")
const router = express.Router()
const pool = require("../../db/db.js")
const Response = require("../../utils/ResponseData.js")

/**
 * 查询所有商家信息
 * 支持分页
 * 请求地址:http://localhost:3000/getAll/:page/:pageSize
 */
router.get("/getAll/:page/:pageSize", (req, res, next) => {
    let { page, pageSize } = req.params
    let startIndex = (page - 1) * pageSize
    pageSize = parseInt(pageSize)
    const sql = "select * from business order by b_score desc limit ?,?"
    pool.querySync(sql, [startIndex, pageSize])
        .then(result => {
            if (result.length > 0) {
                res.send(Response.success(result, "日志查询成功!"))
            } else {
                res.send(Response.error("日志查询失败!"))
            }
        }).catch(err => {
            if (err) {
                console.log("/getAll", err);
                next(err)
                return
            }
        })
})
/**
 * 根据id查询商家信息
 * 请求地址:http://localhost:3000/
 */
router.get("/getById/:id", (req, res, next) => {
    let { id } = req.params
    const sql = "select * from business where b_id = ?"
    pool.query(sql, [id], (err, result) => {
        if (err) {
            console.log("/getById", err);
        }
        if (result.length > 0) {
            res.send(Response.success(result, "日志查询成功!"))
        } else {
            res.send(Response.error("日志查询失败!"))
            next(err)
            return
        }
    })
})

module.exports = router